<?php
class CompTalent extends AppModel
{
	var $name='CompTalent';
	var $primaryKey = 'id'; 
	 var $order = 'CompTalent.declare_time desc';
	
	var $belongsTo = array(
		'Talstack'=>array(
			'conditions' => array("Talstack.is_deleted='0'"),
			'fields'=>'Talstack.gov',
			'className' => 'Talstack',
			'foreignKey' => 'type_id',
		),
		'Company'=>array(
			'conditions' => array("Company.is_deleted='0'"),
			'className'=>'Company',
			'foreignKey'=>'cid'	
		)		
	);

	var $hasMany = array(
			'Attachment' => array(
			'conditions' => array("Attachment.is_deleted='0'"),
			'className' => 'Attachment',
			'foreignKey' => 'pid'
			)
	);

	
// function getsubtype($id)
//    {
//    	$where="";
//    	$where.=" Talstack.super_class='$id' ";
//    	$where.="and Talstack.is_deleted='0'";
//    	$objtype=$this->findAll($where);
//    	return $objtype;
//    }
	
	function alertmessage()
	{
		$isNeedToAlert = false;
		$this->unbindModel(array ('hasMany' => array ('Attachment')));
		$declare_times = $this->find("all",array('conditions'=>array("CompTalent.is_deleted='0'"),
												'fields'=>array("CompTalent.id,CompTalent.declare_time")			
											));
		foreach($declare_times as $declare_time)
		{
			$talent_declare_time = $declare_time["CompTalent"]["declare_time"];
			$talent_id = $declare_time["CompTalent"]["id"];
			$months = intval((time()-strtotime($talent_declare_time))/30/24/3600);
			if($months >= 11)
			{
				$isNeedToAlert = true;
				App::import('Helper', 'Accp');
				$accp = new AccpHelper();
				$data=array("guid"=>$talent_id,"event"=>"talent_onemonth_left","usertype"=>"1","isNeedToAlert"=>$isNeedToAlert);
				$isflag=$accp->accpalert($data);
			}
		}
		
	}
	
	/**
	* 获取申报常熟市海内外领军人才公司的id
	* @param: $type = 0,返回所有申报常熟海内外领军人才公司的ID
	* 		  $type = 1,返回所有已立项和未立项的常熟海内外领军人才公司的ID
	* 		  $type = 2,返回所有已立项的常熟海内外领军人才公司的ID
	* @Return: String
	*/
 	function getCSLeadCompanyId($type)
    {
    	$tname = classregistry::init('Constant')->LeadTalent;
    	classregistry::init('Talstack')->gettalentpname($tname);
    	$idlist = classregistry::init('Talstack')->getTalstackidlist();
    	$this->unbindModel(array ('hasMany' => array ('Attachment')));
    	
    	
		$father_name =classregistry::init('Constant')->Declarate_Status;
		$type_name = classregistry::init('Constant')->SuccessDeclare;
		$talent_delcare_success_id = classregistry::init('Codetype')->findCodeValueID($father_name,$type_name);//常熟海内外领军人才申报已立项的ID
		$type_name = classregistry::init('Constant')->FailDeclare;
		$talent_delcare_fail_id = classregistry::init('Codetype')->findCodeValueID($father_name,$type_name);//常熟海内外领军人才申报未立项的ID
    	
    	$where = "CompTalent.is_deleted='0' and CompTalent.type_id in ($idlist)";
    	if($type == 1)
    		$where .= " and CompTalent.declarate_status in ('$talent_delcare_success_id','$talent_delcare_fail_id')";
    	else if($type == 2)
    		$where .= " and CompTalent.declarate_status = '$talent_delcare_success_id'";
    	$companyid = $this->find("all",array('conditions'=>array($where),
											 'fields'=>array("CompTalent.cid")			
								));
		$cidlist = "";
		foreach($companyid as $company)
		{
			$id = $company["CompTalent"]["cid"];
			$cidlist .= "'".$id."',";
		}
		$cidlist = rtrim($cidlist,",");
    	return $cidlist;
    }
    
    //获取企业人才项目
    function getTalent($successid){
    	
    	$sql="SELECT type_id FROM `comp_talents` WHERE declarate_status='$successid' and `is_deleted`='0' and type_id<>'' group by type_id ";
    	$objtalent = $this->query($sql);
    	$tidlis=array();
    	foreach ($objtalent as $talent){
    		$tvalue=$talent["comp_talents"]["type_id"];
    		classregistry::init('Talstack')->tid="";
			classregistry::init('Talstack')->showtname($tvalue);
			$tid=classregistry::init('Talstack')->tid;
			if($tid){
				$objtid=explode("/",$tid);
				$tidlis[]=$objtid[0];
			}
    	}
    	if($tidlis){
    		$tidlis=array_unique($tidlis);
    	}
    	return $tidlis;
    }
    
    function getCidByTypeid($typeid){
    	$where = "CompTalent.is_deleted='0' and CompTalent.type_id in ($typeid)";
    	$companyid = $this->find("all",array('conditions'=>array($where),
											 'fields'=>array("CompTalent.cid")			
								));
		$cidlist = "";
		foreach($companyid as $company)
		{
			$id = $company["CompTalent"]["cid"];
			$cidlist .= "'".$id."',";
		}
		$cidlist = rtrim($cidlist,",");
    	return $cidlist;
    	
    }
    
    
    
    
    
}
?>